﻿#include "Sprite.h"
#include "Global.h"

CSprite::CSprite()
{
	SpriteTexture = NULL;
	Start = 0;
	End = 0;
	Index = 0;
	TimeAni = 0;
	TimeLocal = 0;
}

CSprite::CSprite(const CSprite &sprite)
{
	SpriteTexture = sprite.SpriteTexture;
	Start = sprite.Start;
	End = sprite.End;
	TimeAni = sprite.TimeAni;
	Index = sprite.Start;
	TimeLocal = sprite.TimeLocal;
}

//Khởi tạo 1 sprite có thể điều khiển
CSprite::CSprite(CTexture* texture, int start, int end, int timeAni)
	: SpriteTexture(texture)
{
	Start = start;
	End = end;
	TimeAni = timeAni;
	Index = start;
	TimeLocal = 0;
}

//Khởi tạo một sprite động hiệu ứng hoạt hình
CSprite::CSprite(CTexture* texture, int timeAni)
	: SpriteTexture(texture)
{
	Start = 0;
	End = texture->Count - 1;
	TimeAni = timeAni;
	Index = 0;
	TimeLocal = 0;
}

void CSprite::Next()
{
	Index++;
	if(Index > End)
	{
		Index = Start;
	}
}

void CSprite::Reset()
{
	Index = Start;
	TimeLocal = 0;
}

void CSprite::SelectFrameOf(int index)
{
	Index = index;
}

void CSprite::UpdateEffect(int elapsedTime)
{
	TimeLocal += elapsedTime;

	if(TimeLocal >= TimeAni)
	{
		TimeLocal = 0;
		Next();
	}
}

void CSprite::Draw(int x, int y)
{
	RECT srcRect;

	srcRect.left = (Index % SpriteTexture->Cols) * (SpriteTexture->FrameWidth);
	srcRect.top = (Index / SpriteTexture->Rows) * (SpriteTexture->FrameHeight);
	srcRect.right = srcRect.left + SpriteTexture->FrameWidth;
	srcRect.bottom = srcRect.top + SpriteTexture->FrameHeight;

	D3DXVECTOR3 pos((float)x, (float)y, 0);

	G_SpriteHandler->Draw(
		SpriteTexture->Texture,
		&srcRect,
		NULL,
		&pos, 
		0xFFFFFFFF);
}

void CSprite::DrawFlipX(int x, int y)
{
	D3DXMATRIX oldMatrix;
	G_SpriteHandler->GetTransform(&oldMatrix);

	D3DXMATRIX newMatrix;
	D3DXVECTOR2 center = D3DXVECTOR2(x + SpriteTexture->FrameWidth / 2, y + SpriteTexture->FrameHeight / 2);
	D3DXVECTOR2 rotate = D3DXVECTOR2(-1, 1);

	D3DXMatrixTransformation2D(&newMatrix, &center, 0.0f, &rotate, NULL, 0.0f, NULL);
	D3DXMATRIX finalMatrix = newMatrix * oldMatrix;
	G_SpriteHandler->SetTransform(&finalMatrix);

	Draw(x, y);

	G_SpriteHandler->SetTransform(&oldMatrix);
}

void CSprite::DrawFlipY(int x, int y)
{
	D3DXMATRIX oldMatrix;
	G_SpriteHandler->GetTransform(&oldMatrix);

	D3DXMATRIX newMatrix;
	D3DXVECTOR2 center = D3DXVECTOR2(x + SpriteTexture->FrameWidth / 2, y + SpriteTexture->FrameHeight / 2);
	D3DXVECTOR2 rotate = D3DXVECTOR2(1, -1);

	D3DXMatrixTransformation2D(&newMatrix, &center, 0.0f, &rotate, NULL, 0.0f, NULL);
	D3DXMATRIX finalMatrix = newMatrix * oldMatrix;
	G_SpriteHandler->SetTransform(&finalMatrix);

	Draw(x, y);

	G_SpriteHandler->SetTransform(&oldMatrix);
}

void CSprite::DrawRect(int x, int y, RECT srcRect)
{
	D3DXVECTOR3 pos((float)x, (float)y, 0);

	G_SpriteHandler->Draw(
		SpriteTexture->Texture,
		&srcRect, 
		NULL, 
		&pos, 
		D3DCOLOR_XRGB(255, 255, 255));
}

void CSprite::DrawTransform(int x, int y, D3DXVECTOR2 scale, float degRotate, float depth)
{
	RECT srcRect;

	srcRect.left = (Index % SpriteTexture->Cols) * (SpriteTexture->FrameWidth);
	srcRect.top = (Index / SpriteTexture->Rows) * (SpriteTexture->FrameHeight);
	srcRect.right = srcRect.left + SpriteTexture->FrameWidth;
	srcRect.bottom = srcRect.top + SpriteTexture->FrameHeight;

	D3DXMATRIX oldMatrix;
	G_SpriteHandler->GetTransform(&oldMatrix);

	D3DXMATRIX newMatrix;
	D3DXVECTOR2 center = D3DXVECTOR2(x + SpriteTexture->FrameWidth / 2, y + SpriteTexture->FrameHeight / 2);

	D3DXMatrixTransformation2D(
		&newMatrix, 
		&center, 
		0.0f, 
		&scale, 
		&center, 
		D3DXToRadian(degRotate), 
		NULL);

	D3DXMATRIX finalMatrix = newMatrix * oldMatrix;
	G_SpriteHandler->SetTransform(&finalMatrix);

	G_SpriteHandler->Draw(
		SpriteTexture->Texture,
		&srcRect,
		NULL, 
		&D3DXVECTOR3(x, y, depth),
		0xFFFFFFFF);

	G_SpriteHandler->SetTransform(&oldMatrix);
}
